package config

import (
	"log"
	"mywails/main/code/config/global"
	"time"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

func initDB() {
	path := config.Database.Path
	db, err := gorm.Open(sqlite.Open(path), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{SingularTable: true}, // 表名不加复数
	})
	if err != nil {
		log.Fatalf("failed to connect database, got error: %v", err)
	}
	sqlDB, err := db.DB()
	sqlDB.SetMaxIdleConns(config.Database.MaxIdleConns)
	sqlDB.SetMaxOpenConns(config.Database.MaxOpenConns)
	sqlDB.SetConnMaxLifetime(time.Hour)
	if err != nil {
		log.Fatalf("failed to get database connection, got error: %v", err)
	}
	global.DB = db
}
